// pages/discount/BrandCoupon.js
var Zan = require('../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start = 0;
Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    brandCouponList: [],
    brandId: null,
    status: null,
    title: null,
    brandName:null,
    nodata: {
      msg: '暂无数据',
      notype: 1,
      imgurl: '/images/nodatas.png',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.setData({
      brandId: options.brandId,
      status: options.status,
      brandName: decodeURIComponent(options.name)
    })

    wx.setNavigationBarTitle({
      title: decodeURIComponent(options.name),
    })

    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.setData({
      brandCouponList: []
    })
    start = 0;
    // staus,0 未上架，1，使用中
    network._post_page_json(this, api.userBrandCouponList, start, { brandId: this.data.brandId, status: this.data.status }, brandsuccess);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.brandCouponList.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.userBrandCouponList, start, { brandId: this.data.brandId, status: this.data.status }, brandsuccess);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  goCouponDetails: function (e) {
    var id = e.currentTarget.dataset.id;
    // console.log(id);
    wx.navigateTo({
      url: '/pages/discount/CouponDetails' + '?couponId=' + id,
    })
  }
}))

var brandsuccess = function (that, res) {
  console.log(res.data.data);
  if (res.data.data) {
    that.setData({
      brandCouponList: that.data.brandCouponList.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
  }
}